package cn.icyzx.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.icyzx.service.EmailService;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import cn.icyzx.service.UserService;

/**
 * Created by 郑凯 on 2016/8/25.
 */
@Controller
public class EmailProcess {

    /**
     * ActiveUserService contains sendEmail, checkEmail and activeUser
     */
    @Autowired
    EmailService emailService;

    @Autowired
    UserService userService;


    /**
     * 激活用户成功的页面（主页+提示）
     *
     * @param uid   根据uid来激活
     * @param model 重定向参数,可以在下一个处理中保持该参数
     * @throws
     */
    @RequestMapping(value = "/activeUser", method = RequestMethod.GET)
    String activeUser(@RequestParam("uid") String uid, RedirectAttributes model) {
        userService.activeUser(uid);
        model.addFlashAttribute("tip", "您已成功激活你的账号，请登录吧~");
        return "redirect:/";
    }


    /**
     * activeUser（返回字符串，提供优质的视觉效果）
     *
     * @param email 根据所提供的Email,来发送邮件
     * @throws
     */
    @RequestMapping(value = "/activeUser", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    String activeUser(@RequestParam("email") String email) {
        emailService.sendEmailToActive(email);
        return "我们已经向你的邮箱中发送了一封邮件，请注意查收";
    }
}
